package com.example.config;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author mingtian
 * @version V1.0
 * @Description spring 中注册 bean 进行条件判断 可以使用 @Conditional(MyCondition.class) 注解
 * @date 2019/12/9 11:35
 */
public class MyCondition implements Condition {

    private static final String win8 = "Windows 8.1";

    private static final String win10 = "Windows 10";

    /**
     * @param context  获取当前上下文
     * @param metadata 获取当前注解的信息
     * @return
     */
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //获取当前环境信息
        Environment environment = context.getEnvironment();
        String property = environment.getProperty("os.name");
        //判断是否进行注册，true 表示注册；false 表示不注册
        if (win10.equals(property)) {
            return true;
        }
        return false;
    }
}
